<?php
//Data loading
//By Bayu
$pdf=new COAPDFReport("P","mm","Letter");
$pdf->DBConnection($DBConnection->dbc);
$pdf->Period(GetParam("y",""),GetParam("m",""),GetParam("ly",""),GetParam("lm",""));
$pdf->AddPage();
$pdf->GenerateReport();
$pdf->Output();

class COAPDFReport extends PDF {
  /*function COAPDFReport($Page,$Size,$PageMode) {
    parent::PDF($Page,$Size,$PageMode);
    $this->ColumnSize = array(85,40,40,40);
    $this->TableName = "tblcoa";
    $this->AddFont('arialn','','arialn.php');
    $this->SetFont('arialn','',10);
    $this->SetLeftMargin(5);
    $this->TotalIncome  = 0;
    $this->TotalExpense = 0;
       $this->TotalGrandExpense=0;
       $this->TotalGrandIncome=0;

  }*/

  function Header() {
	$this->ColumnSize = array(85,40,40,40);
    $this->AddFont('arialn','','arialn.php');
    $this->SetFont('arialn','',10);
    $this->SetLeftMargin(5);
	
      $title = "PENDAPATAN & BIAYA";
      $page   = 'HAL';
      $title .= (GetParam("m","")=="13")?' (AUDIT)':"";
      $perush = (GetParam("office",""))?GetCompany (GetParam("office","")):'PT. AMPEL JAYA';

        $this->SetFont('arialn','',10);
      $this->Cell(200,5,$perush,0,1,'C',0);
      $this->Cell(200,5,$title,0,1,'C',0);
      $Periode = (GetParam("m","")=="13")?$this->BuildPeriod (GetParam("y",""), 12):$this->BuildPeriod (GetParam("y",""), GetParam("m",""));
      $this->Cell(200,5,"DATA MUTASI",0,1,'C',0);
      $this->Cell(200,5,"PERIODE: ".$Periode,0,1,'C',0);
      $this->Cell(200,5,$page." ".$this->PageNo(),0,1,'R',0);

        //Column widths
        $this->SetFont('arialn','',10);
        $this->Cell($this->ColumnSize[0],7,"UNIT USAHA","TB",0,'C',0);
        $this->Cell($this->ColumnSize[1],7,"PENDAPATAN","TB",0,'C',0);
        $this->Cell($this->ColumnSize[2],7,"BIAYA","TB",0,'C',0);
        $this->Cell($this->ColumnSize[3],7,"HASIL","TB",0,'C',0);
        $this->Ln();
        $this->SetFont('arialn','',8);
  }

  function BuildPeriod ($year, $month) {
    $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
    return strtoupper(date("d ", mktime(0, 0, 0, $month+1, 0, $year))).$arraymonth[$month].strtoupper(date(" Y", mktime(0, 0, 0, $month+1, 0, $year)));
  }

  function Footer() {
    $this->Cell(205,0,'','T');
  }

  function Period($Year, $Month, $LastYear, $LastMonth){
    if ($Month<10) $Month = "0".$Month;
    if (($Month-1)<10) {$LMonth="0".($Month-1);}
    else $LMonth=$Month-1;
    if ($LastMonth<10) $LastMonth = "0".$LastMonth;
    $this->_CYear  = $Year;
    $this->_CMonth = $Month;
    $this->_SMonth = "aku".$Month;
    $this->_LastYear = $LastYear;
    $this->_LastMonth = "aku".$LastMonth;
    if ($Month==1) $this->_LSMonth = "awal";
    else $this->_LSMonth = "aku".$LMonth;
  }

  function DBConnection ($Conn) {
    $this->DBConnection = $Conn;
  }

  function GenerateReport() {
    $SQL = "SELECT * FROM rpt_income_vs_expense ORDER BY urut ";
    $MyResults = $this->DBConnection->get_results($SQL,ARRAY_A);

    foreach ($MyResults as $RowResults){
//      $this->BlankLine();
      $this->WriteLine($RowResults["desc_uu"], "", "", "", 0);
// Skada income
      $this->GenerateDetail($RowResults["skada"],$RowResults["skadatype"],$RowResults["uu"],$this->_CMonth,$this->_CYear);
// Skada expense
      $this->GenerateTotal();
    }

    $this->GenerateGrandTotal();
  }

  function GenerateTotal() {
       $this->WriteLine("TOTAL   ======>", FormatNumber($this->TotalIncome), FormatNumber($this->TotalExpense), FormatNumber($this->TotalIncome-$this->TotalExpense), "TB");
       $this->TotalExpense=0;
       $this->TotalIncome=0;
  }

  function GenerateGrandTotal() {
       $this->WriteLine("GRAND TOTAL   ======>", FormatNumber($this->TotalGrandIncome), FormatNumber($this->TotalGrandExpense), FormatNumber($this->TotalGrandIncome-$this->TotalGrandExpense), "TB");
  }


  function GenerateDetail($skadaline,$skadaposition,$untush,$month,$year) {
    $row_skada = explode(',',$skadaline);
    $row_skada_type = explode(',',$skadaposition);


    //print_r($row_skada);
    for($x=0;$x<count($row_skada);$x++) {
          if (strlen($SQLWhere)) {
            $SQLWhere .= " OR {$this->TableName}.skada like '".$row_skada[$x]."%' ";
          } else $SQLWhere = " {$this->TableName}.skada like '".$row_skada[$x]."%' ";
    }
/*
    $SQL  = "
      SELECT tblcoa.skada as skada,tblcoa.namacc as namacc,
             sum(if(dk='D',ais_memorial.rupiah,0)) as mutasi_debet,
             sum(if(dk='K',ais_memorial.rupiah,0)) as mutasi_kredit,
             sum(if(dk='D',ais_memorial.rupiah,0))-sum(if(dk='K',ais_memorial.rupiah,0)) as saldo_rupiah
      FROM ais_memorial
      INNER JOIN tblcoa ON ais_memorial.skada=tblcoa.skada AND tblcoa.ledger=$year AND ais_memorial.kdact='$untush'
      WHERE ".$SQLWhere." AND month(tglbkt)={$this->_CMonth} AND year(tglbkt)={$this->_CYear}  ";
*/
    $SQL  = "
      SELECT tblcoa.skada as skada,tblcoa.namacc as namacc,
             sum(if(dk='D',ais_memorial.rupiah,0)) as mutasi_debet,
             sum(if(dk='K',ais_memorial.rupiah,0)) as mutasi_kredit,
             sum(if(dk='D',ais_memorial.rupiah,0))-sum(if(dk='K',ais_memorial.rupiah,0)) as saldo_rupiah
      FROM ais_memorial
      INNER JOIN tblcoa ON ais_memorial.skada=tblcoa.skada AND tblcoa.ledger=$year AND ais_memorial.kdact='$untush'
      WHERE (ais_memorial.skada like '6%' or ais_memorial.skada like '7%' or ais_memorial.skada like '8%' or
     ais_memorial.skada like '9%') AND month(tglbkt)={$this->_CMonth} AND year(tglbkt)={$this->_CYear}  ";


    $SQL .= " GROUP BY ais_memorial.skada ORDER by skada ";
//    print $SQL."<p>\n";
    $MyResults = $this->DBConnection->get_results($SQL,ARRAY_A);

    if ($MyResults) {
      foreach ($MyResults as $RowResults){

    $this->WriteLine("   ".$RowResults["namacc"], FormatNumber($RowResults["mutasi_kredit"]), FormatNumber($RowResults["mutasi_debet"]), FormatNumber($RowResults["mutasi_kredit"]-$RowResults["mutasi_debet"]), 0);
    $this->TotalIncome += $RowResults["mutasi_kredit"];
    $this->TotalExpense += $RowResults["mutasi_debet"];
    $this->TotalGrandExpense+= $RowResults["mutasi_debet"];
    $this->TotalGrandIncome += $RowResults["mutasi_kredit"];

/*
        $keys = array_search(substr($RowResults["skada"],1,1),$row_skada);
        if (strlen($keys) && ($row_skada_type[$keys]=='-')) {
         // $this->WriteLine("   ".$RowResults["namacc"], FormatNumber($RowResults["saldo_rupiah"]), 0, FormatNumber($RowResults["saldo_rupiah"]-0), 0);
    $this->WriteLine("   ".$RowResults["namacc"], FormatNumber($RowResults["mutasi_kredit"]), FormatNumber($RowResults["mutasi_debet"]), FormatNumber($RowResults["mutasi_kredit"]-$RowResults["mutasi_debet"]), 0);
          //$this->TotalIncome += $RowResults["saldo_rupiah"];
    $this->TotalIncome += $RowResults["mutasi_kredit"];
        } else {
        //  $this->WriteLine("   ".$RowResults["namacc"], 0, FormatNumber($RowResults["saldo_rupiah"]), FormatNumber(0-$RowResults["saldo_rupiah"]), 0);
    $this->WriteLine("   ".$RowResults["namacc"], FormatNumber($RowResults["mutasi_kredit"]), FormatNumber($RowResults["mutasi_debet"]), FormatNumber($RowResults["mutasi_kredit"]-$RowResults["mutasi_debet"]), 0);
          //$this->TotalExpense += $RowResults["saldo_rupiah"];
    $this->TotalExpense += $RowResults["mutasi_debet"];
        }
*/
      } // foreach
    } // if result

  }

  function WriteLine($desc_untush, $income, $expense, $result, $isblock="0") {
    if ($isblock<>"0") $border = $isblock;
    $this->Cell($this->ColumnSize[0],5,$desc_untush,$border,0,'L',0);
    $this->Cell($this->ColumnSize[1],5,$income,$border,0,'R',0);
    $this->Cell($this->ColumnSize[2],5,$expense,$border,0,'R',0);
    $this->Cell($this->ColumnSize[3],5,$result,$border,0,'R',0);
    $this->Ln();
  }

  function BlankLine() {
    $this->WriteLine("","","","","");
  }

  function GetTotalRow($parent_skada) {
    $SQL = "

    SELECT a.skada, b.{$this->_LastMonth} as awal,a.{$this->_SMonth} as currmonth,a.{$this->_LSMonth} as lastmonth
    FROM {$this->TableName} a
    LEFT JOIN {$this->TableName} b ON a.skada=b.skada AND b.ledger={$this->_LastYear}
    WHERE a.ledger='{$this->_CYear}' and a.parent='$parent_skada'

    ";
    $MyResults = $this->DBConnection->get_results($SQL,ARRAY_A);
    if ($MyResults) {
       foreach ($MyResults as $RowResults){
          $ResultTotalRow    = $this->GetTotalRow($RowResults['skada']);
          $Rows['currmonth'] = $Rows['currmonth']+ $ResultTotalRow['currmonth'] + $RowResults['currmonth'];
          $Rows['lastmonth'] = $Rows['lastmonth'] + $ResultTotalRow['lastmonth'] + $RowResults['lastmonth'];
          $Rows['awal']      = $Rows['awal'] + $ResultTotalRow['awal'] + $RowResults['awal'];
          $Rows['mutasi']    = $Rows['currmonth'] - $Rows['lastmonth'];
       }
    }
    return $Rows;
  }

  function WriteRow($parent_skada,$inverse) {
     $SQL = "SELECT namacc,skada FROM {$this->TableName} WHERE ledger='{$this->_CYear}' and parent='$parent_skada' ";
     //die($SQL);
     $MyResults = $this->DBConnection->get_results($SQL,ARRAY_A);
     if ($MyResults) {
       foreach ($MyResults as $RowResults){
         $TotalRow = $this->GetTotalRow($RowResults['skada']);
         //print  "{$RowResults['namacc']},{$TotalRow['lastmonth']},{$TotalRow['mutasi']},{$TotalRow['currmonth']},$Last_Year";
         if ($inverse==1) {
            $this->WriteLine("   ".$RowResults['namacc'],FormatNumber($TotalRow['lastmonth']*-1),FormatNumber($TotalRow['mutasi']*-1),FormatNumber($TotalRow['currmonth']*-1),FormatNumber($TotalRow['awal']*-1));
         } else
            $this->WriteLine("   ".$RowResults['namacc'],FormatNumber($TotalRow['lastmonth']),FormatNumber($TotalRow['mutasi']),FormatNumber($TotalRow['currmonth']),FormatNumber($TotalRow['awal']));

        $TotalLast = $TotalLast + $TotalRow['lastmonth'];
        $TotalMutasi = $TotalMutasi + $TotalRow['mutasi'];
        $TotalCurr = $TotalCurr + $TotalRow['currmonth'];
        $TotalLastYear = $TotalLastYear + $TotalRow['awal'];
       }
       $TotalAccount['lastmonth'] = $TotalLast;
       $TotalAccount['mutasi'] = $TotalMutasi;
       $TotalAccount['currmonth'] = $TotalCurr;
       $TotalAccount['awal'] = $TotalLastYear;
       return $TotalAccount;
     }
  }

}
?>
